<?php
namespace app\api\controller;

class Product extends Base
{

    public function _initialize()
    {
        parent::_initialize();
        // $this->chkToken();
    }
    // 获取分类
    public function get_pools()
    {
        $post = array();
        $data = $this->sendInfo('categoryThree', $post);
        $return['result'] = array();
        foreach ($data['data'] as $key => $val) {
            $temp['id'] = $val['api_option'];
            $temp['name'] = $val['category_name'];
            array_push($return['result'], $temp);
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function skus()
    {
        if (! isset($this->request['catalog_id'])) {
            $return['success'] = false;
            $return['desc'] = '没有传入分类池编号';
            $this->ajaxReturn($return);
        }
        if ($this->request['catalog_id'] == '') {
            $return['success'] = true;
            $return['result'] = [];
            $this->ajaxReturn($return);
        }
        $post_category['api_option'] = $this->request['catalog_id'];
        $get_category = $this->sendInfo('apiCategory', $post_category);
        $category_sign3 = array();
        if (count($get_category['data']) == 0) {
            $return['success'] = true;
            $return['result'] = [];
            $this->ajaxReturn($return);
        }
        foreach ($get_category['data'] as $key => $val) {
            array_push($category_sign3, $val['category_sign']);
        }
        $post['where']['category_sign3'] = $category_sign3;
        $post['page']['page'] = 1;
        $post['page']['limit'] = 10000;
        $post['sort']['_o'] = 'create_time';
        $post['sort']['_b'] = 'asc';
        $post['no_need_handle_data'] = 'yes';
        $data = $this->sendInfo('goodsList', $post);
        $return['result'] = array();
        foreach ($data['data'] as $key => $val) {
            array_push($return['result'], $val['goods_sku']);
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function detail()
    {
        if (! isset($this->request['sku'])) {
            $return['success'] = false;
            $return['desc'] = '没有传入商品编号';
            $this->ajaxReturn($return);
        }
        $post['goods_sku'] = $this->request['sku'];
        $post['member_id'] = $this->member_id;
        $post['site_id'] = $this->ys_config['siteId'];
        // 设置采集状态1
        $post['api_get_add_set'] = 1;
        $post['no_more_price'] = 'yes';
        $data = $this->sendInfo('goodsOnlyDetail', $post);
        if (! isset($data['data']['goods_id'])) {
            $return['success'] = false;
            $return['desc'] = '商品已删除';
            $this->ajaxReturn($return);
        }
        $e = $data['data'];
        $temp['sku'] = $e['goods_sku'];
        $temp['producttype'] = strstr($temp['sku'], 'SKU')?0:1;
        $temp['url'] = $this->site_url . 'goods_detail.html?goods_sku=' . $temp['sku'];
        $temp['model'] = $e['goods_model'];
        $temp['weight'] = $e['weight'] . '克';
        $temp['image_path'] = $e['goods_picture']['save_path'];
        $temp['state'] = $e['sale_state'] == 1 ? 1 : 0;
        $temp['brand_id'] = $e['brand_sign'];
        $temp['brand_name'] = $e['brand_name'];
        $temp['name'] = $e['goods_name'];
        $temp['unit'] = $e['goods_unit'];
        $temp['category'] = $e['category_api_option'];
        $temp['service'] = '无';
        $temp['introduction'] = $e['goods_content'];
        $temp['product_area'] = $e['product_area'];
        $temp['upc'] = $e['upc'];
        $temp['code_69'] = '';
        $temp['ware'] = $e['packing_list'];
        $temp['sale_actives'] = 0;
        $attr_head = '';
        $t_param = '<table class="Ptable">';
        $main_property_arigin = array();
        foreach ($e['goods_attr'] as $key => $val) {
            if ($val['group_name'] != $attr_head) {
                $t_param .= '<tr> <th colspan="2">' . $val['group_name'] . '</th></tr>';
                $attr_head = $val['group_name'];
            }
            $t_param .= '<tr> <td class="tdTitle">' . $val['attr_name'] . '</td> <td>' . $val['attr_value'] . '</td> </tr>';
            /*
             * if($val['group_name'] == '基础参数'){
             * $main_property_arigin[$val['attr_name']] = $val['attr_value'];
             * }
             */
            $t['attributeID'] = '';
            $t['attributeName'] = $val['attr_name'];
            $t['valueID'] = '';
            $t['value'] = $val['attr_value'];
            array_push($main_property_arigin, $t);
            $value = $val['attr_value'];
            switch ($val['attr_name']) {
                case '简要配置':
                    $temp['briefconfig'] = $value;
                    break;
                case '包装清单':
                    $temp['ware'] = $value;
                    break;
                case '节能':
                    $temp['energy'] = $value;
                    break;
                case '环保':
                    $temp['protection'] = $value;
                    break;
                case '是否是进口产品':
                    $temp['Imported'] = $value;
                    break;
                case '预计几天内到货':
                    $temp['deliveryDay'] = $value;
                    break;
                case '节能品牌':
                    $temp['EfficientGoodsBrand'] = $value;
                    break;
                case '节能型号':
                    $temp['EfficientGoodsXinhao'] = $value;
                    break;
                case '节能标志认证证书号':
                    $temp['EfficientGoodsSn'] = $value;
                    break;
                case '认证证书有效截至日期':
                    $temp['EfficientGoodsExpiry'] = $value;
                    break;
                case '环保品牌':
                    $temp['EnvmFriendlyGoodsBrand'] = $value;
                    break;
                case '环保型号':
                    $temp['EnvmFriendlyGoodsXinhao'] = $value;
                    break;
                case '环保标志认证证书号':
                    $temp['EnvmFriendlyGoodsSn'] = $value;
                    break;
                case '环保证书有效截至日期':
                    $temp['EnvmFriendlyGoodsExpiry'] = $value;
                    break;
                case '计算机配置':
                    $temp['pc_param'] = $value;
                    break;
                case '节能产品证书编号':
                    $temp['EfficientGoodsCardNum'] = $value;
                    break;
                case '节能产品证书图片':
                    $temp['EfficientGoodsImagePath'] = $value;
                    break;
                case '节能产品证书认证机构':
                    $temp['EfficientGoodsCardOrgan'] = $value;
                    break;
                case '环保产品证书编号':
                    $temp['EnvmFriendlyGoodsCardNum'] = $value;
                    break;
                case '环保产品证书图片':
                    $temp['EnvmFriendlyGoodsImagePath'] = $value;
                    break;
                case '环保产品证书认证机构':
                    $temp['EnvmFriendlyGoodsCardOrgan'] = $value;
                    break;
                case '服务':
                    $temp['service'] = $value;
                    break;
                case '特色商品':
                    if ($value == '非特色商品') {
                        $temp['is_cabinet'] = 0;
                    } elseif ($value == '货柜机') {
                        $temp['is_cabinet'] = 1;
                    } else {
                        $temp['is_cabinet'] = 2;
                    }
                    break;
            }
        }
        $temp['attributes'] = $main_property_arigin;
        /*
         * if($e['api_option'] == 'A0101'){
         * $temp['pc_param'] = $main_property_arigin;
         * }
         */
        $t_param .= '</table>';
        $temp['param'] = $t_param;
        /*
         * $temp['sale_actives'] = 0;
         * $temp['energy'] = '否';
         * $temp['protection'] = '否';
         * $temp['briefconfig'] = '';
         * $temp['ware'] = '';
         * $temp['deliveryDay'] = 3;
         * $temp['Imported'] = '非进口产品';
         */
        $return['result'] = $temp;
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function product_attributes()
    {
        if (! isset($this->request['sku'])) {
            $return['success'] = false;
            $return['desc'] = '没有传入商品编号';
            $this->ajaxReturn($return);
        }
        $post['goods_sku'] = $this->request['sku'];
        $post['member_id'] = $this->member_id;
        $post['site_id'] = $this->ys_config['siteId'];
        // 设置采集状态1
        $post['no_more_price'] = 'yes';
        $post['no_need_goods_handle'] = 'yes';
        $data = $this->sendInfo('goodsOnlyDetail', $post);
        $temp = array();
        $return['result'] = array();
        foreach ($data['data']['goods_attr'] as $key => $val) {
            array_push($temp, $val['attr_name'] . ':' . $val['attr_value']);
        }
        $return['result']['attributes'] = implode(',', $temp);
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function shelf_states()
    {
        if (! isset($this->request['sku'])) {
            $return['success'] = false;
            $return['desc'] = '没有传入商品编号';
            $this->ajaxReturn($return);
        }
        $post['where']['goods_sku'] = explode(',', $this->request['sku']);
        $post['page']['page'] = 1;
        $post['page']['limit'] = 10000;
        $post['sort']['_o'] = 'create_time';
        $post['sort']['_b'] = 'desc';
        $post['no_need_handle_data'] = 'yes';
        $post['sale_state'] = 'all';
        $data = $this->sendInfo('goodsList', $post);
        $return['result'] = array();
        foreach ($data['data'] as $key => $val) {
            $temp['sku'] = $val['goods_sku'];
            $temp['state'] = $val['sale_state'] == 1 ? 1 : 0;
            array_push($return['result'], $temp);
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function images()
    {
        if (! isset($this->request['sku'])) {
            $return['success'] = false;
            $return['desc'] = '没有传入商品编号';
            $this->ajaxReturn($return);
        }
        $post['where']['goods_sku'] = explode(',', $this->request['sku']);
        $post['page']['page'] = 1;
        $post['page']['limit'] = 10000;
        $post['sort']['_o'] = 'create_time';
        $post['sort']['_b'] = 'desc';
        $data = $this->sendInfo('goodsList', $post);
        $return['result'] = array();
        foreach ($data['data'] as $key => $val) {
            $temp['sku'] = $val['goods_sku'];
            $img_sort = 0;
            $temp['images'] = array();
            foreach ($val['goods_pictures'] as $k => $v) {
                $t1['order'] = $img_sort ++;
                $t1['path'] = $v['save_path'];
                array_push($temp['images'], $t1);
            }
            array_push($return['result'], $temp);
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function prices()
    {
        if (! isset($this->request['sku'])) {
            $return['success'] = false;
            $return['desc'] = '没有传入商品编号';
            $this->ajaxReturn($return);
        }
        $post['goods_sku'] = explode(',', $this->request['sku']);
        $post['site_id'] = $this->ys_config['siteId'];
        $data = $this->sendInfo('goodsPrice', $post);
        $return['result'] = array();
        foreach ($data['data'] as $key => $val) {
            $temp['sku'] = $val['goods_sku'];
            $temp['price'] = $val['api_price'];
            $temp['mall_price'] = $val['sale_price'];
            array_push($return['result'], $temp);
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function stocks()
    {
        if (! isset($this->request['sku'])) {
            $return['success'] = false;
            $return['desc'] = '没有传入商品编号';
            $this->ajaxReturn($return);
        }
        $post['goods_sku'] = explode(',', $this->request['sku']);
        $post['site_id'] = $this->ys_config['siteId'];
        $data = $this->sendInfo('goodsPrice', $post);
        $return['result'] = array();
        foreach ($data['data'] as $key => $val) {
            $temp['sku'] = $val['goods_sku'];
            $temp['area'] = $this->request['area'];
            $temp['desc'] = '有货';
            $temp['num'] = $val['view_stock'];
            array_push($return['result'], $temp);
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function addNewSkus()
    {
        $post['member_id'] = $this->member_id;
        $post['where']['api_get_add'] = 'yes';
        $post['sort']['_o'] = 'create_time';
        $post['sort']['_b'] = 'asc';
        $post['page']['page'] = 1;
        $post['page']['limit'] = 10000;
        $post['no_need_handle_data'] = 'yes';
        $post['site_id'] = $this->ys_config['siteId'];
        $data = $this->sendInfo('goodsList', $post);
        $return['result'] = array();
        if ($data['count'] > 0) {
            foreach ($data['data'] as $key => $val) {
                array_push($return['result'], $val['goods_sku']);
            }
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function editSkus()
    {
        $post['member_id'] = $this->member_id;
        $post['where']['api_get_add'] = 'yes';
        $post['sort']['_o'] = 'goods_id';
        $post['sort']['_b'] = 'asc';
        $post['page']['page'] = 1;
        $post['page']['limit'] = 10000;
        $post['no_need_handle_data'] = 'yes';
        $post['site_id'] = $this->ys_config['siteId'];
        $data = $this->sendInfo('goodsList', $post);
        $return['result'] = array();
        if ($data['count'] > 0) {
            foreach ($data['data'] as $key => $val) {
                array_push($return['result'], $val['goods_sku']);
            }
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function getMessagePool()
    {
        $post['post_type'] = $this->request['type'];
        $data = $this->sendInfo('postNoticeList', $post);
        $return['success'] = true;
        if ($data['data']) {
            $return['result'] = array();
            foreach ($data['data'] as $key => $val) {
                $temp = array();
                $temp['id'] = $val['post_notice_id'];
                $temp['type'] = $val['post_type'];
                $temp['time'] = date('Y-m-d H:i:s', time());
                $temp['result'] = array();
                switch ($val['post_type']) {
                    case '2':
                        $temp['result']['skuId'] = $val['sku_order_no'];
                        $temp['result']['state'] = 1;
                        $temp['result']['price'] = $val['new_data'];
                        $temp['result']['market_price'] = $val['old_data'];
                        break;
                    case '4':
                    case '6':
                    case '16':
                        $temp['result']['skuId'] = $val['sku_order_no'];
                        $temp['result']['state'] = $val['new_data'];
                        break;
                    case '5':
                    case '10':
                    case '12':
                        $temp['result']['orderId'] = $val['sku_order_no'];
                        $temp['result']['state'] = 1;
                        break;
                    case '28':
                        $temp['result']['orderId'] = $val['sku_order_no'];
                        $temp['result']['state'] = 4;
                        break;
                }
                array_push($return['result'], $temp);
            }
            $this->ajaxReturn($return);
        } else {
            $return['result'] = [];
            $this->ajaxReturn($return);
        }
    }

    public function delMessagePool()
    {
        $post['post_notice_id'] = $this->request['messageId'];
        $data = $this->sendInfo('postNoticeDelete', $post);
        $return['success'] = true;
        $return['desc'] = '消息' . $this->request['messageId'] . '删除成功';
        $this->ajaxReturn($return);
    }

    public function certificates()
    {
        if (! isset($this->request['sku'])) {
            $return['success'] = false;
            $return['desc'] = '没有传入商品编号';
            $this->ajaxReturn($return);
        }
        $post['where']['goods_sku'] = explode(',', $this->request['sku']);
        $post['page']['page'] = 1;
        $post['page']['limit'] = 10000;
        $post['sort']['_o'] = 'create_time';
        $post['sort']['_b'] = 'desc';
        $data = $this->sendInfo('goodsList', $post);
        $return['result'] = array();
        foreach ($data['data'] as $key => $val) {
            $temp['sku'] = $val['goods_sku'];
            // if($val['is_energy'] == 1){
            $temp['save_energy_cert_no'] = $val['energy_cert_no'];
            $temp['save_energy_cert_image'] = $val['energy_cert_image'];
            $temp['save_energy_cert_indate'] = $val['energy_cert_indate'];
            // }
            // if($val['is_environmental'] == 1){
            $temp['environment_protect_cert_no'] = $val['environment_protect_cert_no'];
            $temp['environment_protect_cert_image'] = $val['environment_protect_cert_image'];
            $temp['environment_protect_cert_indate'] = $val['environment_protect_cert_indate'];
            // }
            array_push($return['result'], $temp);
        }
        
        $return['success'] = true;
        $this->ajaxReturn($return);
    }
}